<template>
	<view>
		<view class="mask" @touchmove.stop="" v-show="show" @click="clased()"></view>

		<view class="work-record" @click.stop="" @touchmove.stop=""
			:style="show ? 'transform: translateY(0rpx);' : 'transform: translateY(1000rpx);'">
			<view class="line" @click="clased()"><text></text></view>
			<view class="content">
				<view class="content_title"> {{formData.id?'编辑无法保养记录':'添加无法保养记录'}}</view>
				<view class="tips required"> 添加图片/视频</view>
				<view class="imgBox">
					<view class="imgBox-item" v-for="(item, index) in imgUrl" :key="index">
						<image @click="preViewFile(item, index)"
							:src="item.type == 'image' ? item.url : item.img || '../../../static/images/video.png'"
							mode="aspectFill"></image>
						<text @click="removeFile(index)">-</text>
					</view>
					<view class="imgBox-item imgBox-add" @click="addfile">
						<image src="@/static/images/camera.png"></image>
						<view>上传</view>
					</view>
				</view>
				<!-- 备注 -->
				<view class="tips required">无法保养原因</view>
				<view class="notice">
					<textarea type="textarea" v-model="remark" height="108rpx"
						:placeholder="'请输入..'" placeholder-class="textare"
						style="font-size: 28rpx;height:108rpx;" maxlength="300" />
					<view class="length">{{ remark.length + '/300' }}</view>
				</view>

				<view class="save"><button hover-class="none" @click="handleSave">提交</button></view>
			</view>
		</view>
	</view>
</template>

<script>
import ImageDragSort from '@/components/image-drag-sort/img-and-video.vue';
import { mapState, mapActions } from 'vuex';
import { setPlanLog } from '@/apis/workOrder.js'; 
export default {
	props: {
		planId: [String, Number],
		show: {
			type: Boolean,
			default: false
		},
		planInfo: {
			type: Object,
			default: () => {
				return {};
			}
		}
	},

	components: {
		ImageDragSort
	},
	data() {
		return { 
			suffix:'',
			formData: {
				files: {},
				records: {},
				remarks: {}
			}, // 历史记录 
			imgUrl: [], // 图片或视频
			remark: '', // 描述 
		};
	},
	watch: {},
	created() { },
	computed: {
		...mapState('user', ['userinfo'])
	},
	methods: {
		initData(item) {  
			this.formData = item; 
			this.imgUrl = JSON.parse(JSON.stringify(item.errorImg || []));
			this.remark = item.errorDesc || ''; 
	 	},
		clased() {
			this.$emit('closed', false);
		},
		// 预览
		preViewFile(item, index) {
			wx.previewMedia({
				sources: this.imgUrl,
				current: index
			});
		},
		// 添加图片或视频
		addfile() {
			if (this.imgUrl.length >= 9) {
				uni.showToast({
					title: '图片/视频最大可上传9个',
					icon: 'none'
				});
				return;
			}
			uni.showActionSheet({
				itemList: ['添加图片', '添加视频'],
				success: res => {
					if (res.tapIndex === 0) {
						uni.showActionSheet({
							itemList: ['拍摄', '从相册选择'],
							success: (dres) => {
								if (dres.tapIndex === 0) {
									try {
										uni.$off('uploadImg');
									} catch (error) {

									}
									uni.$once('uploadImg', async (path, imgId,options) => {
										uni.showLoading({
											title: '上传中'
										});
										let img = await this.uploadFileFuc(path, imgId,options);
										if (img == '') return uni.hideLoading();
										this.imgUrl.push({
											url: img,
											type: 'image'
										});
										uni.hideLoading();
									});

									let planInfo = {
										planId:this.planId
									};
									uni.setStorage({
										key: 'waterMarkMsg',
										data: planInfo,
										success: (success) => {
											uni.navigateTo({
												url: '/pages/camera/camera',
												fail: (err) => {
													console.log(err);
												}
											});
										},
									});

									return;
								}
								if (dres.tapIndex === 1) {
									uni.chooseImage({
										sizeType: ['original'],
										sourceType: ['album'],
										count: 9 - this.imgUrl.length,
										success: async (files) => {
											// console.log('img_files', files,files.tempFilePaths[0]);
											// uni.getImageInfo({
											// 	src:files.tempFilePaths[0],
											// 	success:(res)=>{
											// 		console.log('res', res);
											// 	},
											// 	fail:(fail)=>{
											// 		console.log('fail', fail);
											// 	},
											// })
											uni.showLoading({
												title: '上传中',
												mask: true
											});
											for (let i = 0; i < files.tempFilePaths.length;) {
												let img = await this.uploadFileFuc(files.tempFilePaths[i]);
												if (img == '') return;
												this.imgUrl.push({
													url: img,
													type: 'image'
												});
												i++;
											}

											uni.hideLoading();
										}
									});
								}
							}
						});
					} else if (res.tapIndex === 1) {
						uni.chooseVideo({
							sizeType: ['compressed'],
							compressed: true,
							count: 1,
							success: async files => {
								uni.showLoading({
									title: '上传中',
									mask: true
								});
								console.log('video_files', files);
								let img = '';
								if (files.thumbTempFilePath) {
									img = await this.uploadFileFuc(files.thumbTempFilePath);
									if (img == '') return uni.hideLoading();
								}
								let url = await this.uploadFileFuc(files.tempFilePath);
								if (url == '') return uni.hideLoading();
								this.imgUrl.push({
									img,
									url,
									type: 'video'
								});
								uni.hideLoading();
							}
						});
					}
				}
			});
		},
		// 上传文件
		uploadFileFuc(file, imgId,options) {
			return new Promise(resolve => {
				this.http
					.upload(file, imgId,options)
					.then(res => { 
						resolve(res.data.link);
					})
					.catch(() => {
						uni.showToast({
							title: '预览模式禁止上传数据',
							icon: 'none'
						});
						resolve('');
					});
			});
		},
		changeImg(val) {
			console.log(val, 'val的值');
			this.imgUrl = val;
		},
		removeFile(idx) {
			this.imgUrl.splice(idx, 1);
			this.$forceUpdate();
		},
		handleSave() {
			let errorMsg = '';
			if (!this.remark) {
				errorMsg = '请填写无法保养原因';
			}
			if (this.imgUrl.length === 0) {
				errorMsg = '请选择图片或视频';
			}
			if (errorMsg) {
				uni.showToast({
					title: errorMsg,
					icon: 'none'
				});
				return;
			}
			uni.showLoading({
				title: '保存中',
				mask: true
			});
			this.formData.planId = this.planId;
			this.formData.userId = this.userinfo.user_id;
		    this.formData.errorDesc = this.remark;
			this.formData.errorImg = this.imgUrl;

		   setPlanLog(this.formData).then(async res => {
				uni.hideLoading();
				uni.showToast({
					title: '操作成功',
					icon: 'none'
				});  
				this.$emit('closed');
				this.$emit('addRecord');
			});
		},
		 
	}
};
</script>
<style>
textare {
	height: 200rpx;
}

.textare {
	/* color: red; */
}
</style>
<style lang="scss" scoped>
.mask {
	width: 100%;
	height: 100vh;
	position: fixed;
	top: 0rpx;
	left: 0rpx;
	z-index: 100;
	background-color: $uni-bg-color-mask;
}

.work-record {
	background-color: #fff;
	padding: 20rpx 40rpx 32rpx 40rpx;
	border-radius: 8rpx 8rpx 0rpx 0rpx;
	box-shadow: 0rpx -4rpx 12rpx 0rpx rgba(0, 0, 0, 0.12);
	position: fixed;
	z-index: 101;
	bottom: 0;
	transition: 0.4s all;
	transform: translateY(1000rpx);

	.line {
		margin-bottom: 18rpx;
		display: flex;
		justify-content: center;

		text {
			width: 64rpx;
			height: 10rpx;
			background: #d8d8d8;
			border-radius: 6rpx;
		}
	}

	.content {
		width: 100%;

		.content_title {
			width: 100%;
			font-size: 32rpx;
			font-weight: bold;
			color: #000;
		}

		.tips {
			display: flex;
			color: #000;
			align-items: center;
			margin-bottom: 16rpx;
			margin-top: 20rpx;

			image {
				width: 28rpx;
				height: 28rpx;
				margin-right: 6rpx;
			}
		}
    .required::after{
      content: "*";
      display: inline-block;
      color: #E02020;
      font-size: 28rpx;
    }
		.imgBox {
			display: flex;
			flex-wrap: wrap;
			width: calc(100%);

			&-item {
				width: 112rpx;
				height: 112rpx;
				background: #f7f7f8;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				position: relative;

				image {
					width: 100%;
					height: 100%;
				}

				text {
					position: absolute;
					display: block;
					width: 32rpx;
					height: 32rpx;
					top: -10rpx;
					font-weight: bold;
					font-size: 34rpx;
					line-height: 25rpx;
					border-radius: 50%;
					right: -7rpx;
					text-align: center;
					color: #fff;
					z-index: 9;
					background: #ababab;
				}
			}

			&-add {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				background-color: #fff;
				// border: 1rpx solid #e0e0e0;
				background-color: #f7f7f8;
				flex-wrap: wrap;
				color: $theme-color;
				font-size: 24rpx;

				image {
					width: 40rpx;
					height: 40rpx;
				}

				view {
					width: 100%;
					text-align: center;
				}
			}
		}

		.notice {
			width: 94%;
			padding: 20rpx;
			height: 108rpx;
			border: 2rpx solid #e9e9e9;
			border-radius: 4rpx;
			position: relative;
			padding-bottom: 30rpx;
		}

		textarea {
			font-size: 28rpx;
			width: 100%;
			height: 108rpx;
		}

		.length {
			text-align: right;
			color: #ccc;
			position: absolute;
			right: 10rpx;
			font-size: 24rpx;
			bottom: 5rpx;
		}

		.records {
			display: flex;
			align-items: center;
			font-size: 28rpx;
			justify-content: flex-start;

			input {
				width: 316rpx;
				/* height: 72rpx; */
				border: 2rpx solid #e9e9e9;
				border-radius: 4rpx;
				/* line-height: 72rpx; */
				padding: 12rpx;
			}

			text {
				color: #666666;
				margin-left: 10rpx;
			}
		}
	}

	.save {
		flex: 1;
		display: flex;
		justify-content: center;
		margin-top: 20rpx;

		button {
			display: flex;
			width: 670rpx;
			height: 70rpx;
			// background: #0091ff;
			border-radius: 8rpx;
			// color: #fff;
			justify-content: center;
			align-items: center;
		}
	}
}
</style>
